/*
 * @features: 对于按钮类快速绑定操作数据的功能，并且可以开启限制
 * @description: 说明
 * @Date: 2021-10-02 14:43:17
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2021-10-10 17:38:42
 * @LastEditors: judu233
 */
import c3d from "../../../cc";
import { ProxyOb } from "./ProxyOb";
const { ccclass, property, executeInEditMode, disallowMultiple, menu } = c3d._decorator;

/**限制值边界范围的模式 */
enum CLAMP_MODE { MIN, MAX, MIN_MAX, }


@ccclass(`VmFastCall`)
export default class VmFastCall {
    @property({ displayName: `设置handle调用限制`, tooltip: `按钮组件调用该组件的快速修改函数` })
    valueClamp: boolean = false;

    @property({ displayName: `限制模式`, type: c3d.Enum(CLAMP_MODE), visible(this: VmFastCall) { return this.valueClamp } })
    valueClampMode: CLAMP_MODE = CLAMP_MODE.MIN_MAX;

    @property({ displayName: `最小值`, visible(this: VmFastCall) { return this.valueClamp && this.valueClampMode !== CLAMP_MODE.MAX } })
    valueMin: number = 0;

    @property({ displayName: `最大值`, visible(this: VmFastCall) { return this.valueClamp && this.valueClampMode !== CLAMP_MODE.MIN } })
    valueMax: number = 1;

    add(e: c3d.Event, data: any, watchPath: string, int = false) {
        let a = parseFloat(data);
        let res = ProxyOb.getValue(watchPath, 0) + a;
        if (int) { res = Math.round(res) }
        ProxyOb.setValue(watchPath, this.clampValue(res));
    }

    sub(e: c3d.Event, data: any, watchPath: string, int = false) {
        let a = parseFloat(data);
        let res = ProxyOb.getValue(watchPath, 0) - a;
        if (int) { res = Math.round(res) }
        ProxyOb.setValue(watchPath, this.clampValue(res));
    }

    mul(e: c3d.Event, data: any, watchPath: string, int = false) {
        let a = parseFloat(data);
        let res = ProxyOb.getValue(watchPath, 0) * a;
        if (int) { res = Math.round(res) }
        ProxyOb.setValue(watchPath, this.clampValue(res));
    }

    div(e: c3d.Event, data: any, watchPath: string, int = false) {
        let a = parseFloat(data);
        let res = ProxyOb.getValue(watchPath, 0) / a;
        if (int) { res = Math.round(res) }
        ProxyOb.setValue(watchPath, this.clampValue(res));
    }

    setNum(e, data: any, watchPath: string, int = false) {
        let a = parseFloat(data);
        if (int) { a = Math.round(a) }
        ProxyOb.setValue(watchPath, this.clampValue(a));
    }

    /**限制最终结果的取值范围 */
    clampValue(res) {
        let min = this.valueMin;
        let max = this.valueMax;
        if (!this.valueClamp) return res;
        switch (this.valueClampMode) {
            case CLAMP_MODE.MIN_MAX:
                if (res > max) res = max;
                if (res < min) res = min;
                break;
            case CLAMP_MODE.MIN:
                if (res < min) res = min;
                break;
            case CLAMP_MODE.MAX:
                if (res > max) res = max;
                break;
            default:
                break;
        }
        return res;
    }
}